//package com.duanjw.springboot.es.common;
//
//import cn.hutool.core.bean.BeanUtil;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//
//import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
//import org.elasticsearch.action.delete.DeleteRequest;
//import org.elasticsearch.action.index.IndexRequest;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.action.update.UpdateRequest;
//import org.elasticsearch.client.HttpAsyncResponseConsumerFactory;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.client.indices.CreateIndexRequest;
//import org.elasticsearch.client.indices.CreateIndexResponse;
//import org.elasticsearch.common.settings.Settings;
//import org.elasticsearch.common.xcontent.XContentType;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//
//import javax.annotation.Resource;
//
//import java.io.IOException;
//
//import com.duanjw.springboot.es.config.ElasticsearchProperties;
//
//@Slf4j
//public abstract class BaseElasticsearchService {
//
//    @Resource
//    protected RestHighLevelClient client;
//
//    @Resource
//    private ElasticsearchProperties elasticsearchProperties;
//
//    protected static final RequestOptions COMMON_OPTIONS;
//
//    static {
//        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
//
//        // 默认缓冲限制为100MB，此处修改为30MB。
//        builder.setHttpAsyncResponseConsumerFactory(new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory(30 * 1024 * 1024));
//        COMMON_OPTIONS = builder.build();
//    }
//
//    /**
//     * create elasticsearch index (asyc)
//     *
//     * @param index elasticsearch index
//     * @author fxbin
//     */
//    @SneakyThrows
//    protected void createIndexRequest(String index) {
//        CreateIndexRequest request = new CreateIndexRequest(index);
//        // Settings for this index
//        request.settings(Settings.builder().put("index.number_of_shards", elasticsearchProperties.getIndex().getNumberOfShards())
//                .put("index.number_of_replicas", elasticsearchProperties.getIndex().getNumberOfReplicas()));
//
//        CreateIndexResponse createIndexResponse = client.indices().create(request, COMMON_OPTIONS);
//
//        log.info(" whether all of the nodes have acknowledged the request : {}", createIndexResponse.isAcknowledged());
//        log.info(" Indicates whether the requisite number of shard copies were started for each shard in the index before timing out :{}",
//                createIndexResponse.isShardsAcknowledged());
//    }
//
//    /**
//     * delete elasticsearch index
//     *
//     * @param index elasticsearch index name
//     * @author fxbin
//     */
//    @SneakyThrows
//    protected void deleteIndexRequest(String index) {
//        DeleteIndexRequest deleteIndexRequest = buildDeleteIndexRequest(index);
//        client.indices().delete(deleteIndexRequest, COMMON_OPTIONS);
//    }
//
//    /**
//     * build DeleteIndexRequest
//     *
//     * @param index elasticsearch index name
//     * @author fxbin
//     */
//    private static DeleteIndexRequest buildDeleteIndexRequest(String index) {
//        return new DeleteIndexRequest(index);
//    }
//
//    /**
//     * build IndexRequest
//     *
//     * @param index elasticsearch index name
//     * @param id request object id
//     * @param object request object
//     * @return {@link org.elasticsearch.action.index.IndexRequest}
//     * @author fxbin
//     */
//    protected static IndexRequest buildIndexRequest(String index, String id, Object object) {
//        return new IndexRequest(index).id(id).source(BeanUtil.beanToMap(object), XContentType.JSON);
//    }
//
//    /**
//     * exec updateRequest
//     *
//     * @param index elasticsearch index name
//     * @param id Document id
//     * @param object request object
//     * @author fxbin
//     */
//    @SneakyThrows
//    protected void updateRequest(String index, String id, Object object) {
//        UpdateRequest updateRequest = new UpdateRequest(index, id).doc(BeanUtil.beanToMap(object), XContentType.JSON);
//        client.update(updateRequest, COMMON_OPTIONS);
//    }
//
//    /**
//     * exec deleteRequest
//     *
//     * @param index elasticsearch index name
//     * @param id Document id
//     * @author fxbin
//     */
//    @SneakyThrows
//    protected void deleteRequest(String index, String id) {
//        DeleteRequest deleteRequest = new DeleteRequest(index, id);
//        client.delete(deleteRequest, COMMON_OPTIONS);
//    }
//
//    /**
//     * search all
//     *
//     * @param index elasticsearch index name
//     * @return {@link SearchResponse}
//     * @author fxbin
//     */
//    protected SearchResponse search(String index) {
//        SearchRequest searchRequest = new SearchRequest(index);
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
//        searchRequest.source(searchSourceBuilder);
//        SearchResponse searchResponse = null;
//        try {
//            searchResponse = client.search(searchRequest, COMMON_OPTIONS);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return searchResponse;
//    }
//}